/*
* Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
*/
package com.hyyt.shopjsp.express100.postOrder.controller;

import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.express100.bean.TaskRequest;
import com.hyyt.shopjsp.express100.bean.TaskResponse;
import com.hyyt.shopjsp.express100.callback.demo.HttpRequest;
import com.hyyt.shopjsp.express100.callback.demo.JacksonHelper;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**请求快递100
 * @author: caiang
 * @Date 2016/8/13 11:58
 */
@Controller
@RequestMapping("/call/")
public class CallController extends BaseController {
    private static  final Logger logger = Logger.getLogger(CallController.class);
    private TaskRequest req;
    private TaskResponse resp;
    /**
     * 快递100请求controller
     * @param request
     * @return
     */
    @RequestMapping("askFor")
    @ResponseBody
    public ResultMsg askFor(HttpServletRequest request){
        String path = request.getContextPath();
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
        req.getParameters().put("callbackurl",basePath+"callBack/backMessage.do");//回调地址
        req.setKey("mHdSlmlZ4073");
        HashMap<String, String> p = new HashMap<String, String>();
        p.put("schema", "json");
        p.put("param", JacksonHelper.toJSON(req));
        logger.info("发送信息："+ JacksonHelper.toJSON(req));
        //System.out.println("发送信息："+ JacksonHelper11.toJSON(req));
        try {
            String ret = HttpRequest.postData("http://www.kuaidi100.com/poll", p, "UTF-8");
            resp = JacksonHelper.fromJSON(ret, TaskResponse.class);
            if(resp.getResult()==true){
                logger.info("订阅成功");
                //System.out.println("订阅成功");
                return ResultMsg.create(true);
            }else{
                //System.out.println("订阅失败");
                logger.info("订阅失败");
                 return ResultMsg.create(false);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(false);
        }
    }
}
